package com.hexinfo.dmpro.dev.data.transfer.common.utils;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

public class FileTransferUtil {
    public static File saveFile(MultipartFile file, String basePath, String dbTableName) {
        String path = StrUtil.format("{}/{}/{}/{}/{}.txt", basePath, DateUtil.today(), dbTableName, DateUtil.currentSeconds(), dbTableName);
        FileUtil.touch(path);
        File transFile = new File(path);
        try {
            file.transferTo(transFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return transFile;
    }

    public static File saveFile(MultipartFile file, String basePath) {
        String path = StrUtil.format("{}{}{}_{}.xlsx",
                basePath, File.separator, DateUtil.today(), DateUtil.currentSeconds());
        FileUtil.touch(path);
        File transFile = new File(path);
        try {
            file.transferTo(transFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return transFile;
    }

}
